/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tailyou.androidlearning2426.act

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import android.util.Log
import androidx.compose.runtime.LaunchedEffect
import com.tailyou.androidlearning2426.R
import com.tailyou.androidlearning2426.act.ui.theme.AndroidLearning2400Theme
import com.tailyou.androidlearning2426.data.Datasource
import com.tailyou.androidlearning2426.model.WeChatMsg

class WeChatActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 详细日志
        val dataSource = Datasource()
        val msgList = dataSource.loadWeChatMsg()
        Log.d("WeChatDebug", "数据源中的消息数量: ${msgList.size}")
        msgList.forEachIndexed { index, msg ->
            Log.d("WeChatDebug", "消息$index: ${msg.msgFrom}, 图片ID: ${msg.imageResourceId}")

            // 检查是否是金甲工坊的消息
            if (msg.msgFrom == "金甲工坊") {
                Log.d("WeChatDebug", "金甲工坊的图片ID: ${msg.imageResourceId}")
                Log.d("WeChatDebug", "期望的image2 ID: ${R.drawable.image2}")
                Log.d("WeChatDebug", "是否匹配: ${msg.imageResourceId == R.drawable.image2}")
            }
        }

        setContent {
            AndroidLearning2400Theme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    WeChatMsgApp()
                }
            }
        }
    }
}

@Composable
fun WeChatMsgApp() {
    WeChatMsgList(
        msgList = Datasource().loadWeChatMsg(),
    )
}

@Composable
fun WeChatMsgList(msgList: List<WeChatMsg>, modifier: Modifier = Modifier) {
    LazyColumn(modifier = modifier.padding(top = 56.dp)) {
        items(msgList) { weChatMsg ->
            WeChatMsgItem(weChatMsg = weChatMsg)
        }
    }
}

@Composable
fun WeChatMsgItem(weChatMsg: WeChatMsg) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .background(Color.White)
            .padding(horizontal = 12.dp, vertical = 8.dp)
    ) {
        Image(
            painter = painterResource(weChatMsg.imageResourceId),
            contentDescription = null,
            modifier = Modifier
                .width(56.dp)
                .height(56.dp),
            contentScale = ContentScale.Crop
        )
        Column(
            modifier = Modifier.padding(horizontal = 6.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 2.dp)
            ) {
                Text(
                    text = weChatMsg.msgFrom,
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Medium,
                    color = Color(0xFF333333)
                )
                Text(
                    text = weChatMsg.msgTime, fontSize = 14.sp, color = Color(0xFFAAAAAA)
                )
            }
            Text(
                text = weChatMsg.msgContent, fontSize = 14.sp, color = Color(0xFFAAAAAA)
            )
        }
    }
}

@Preview
@Composable
private fun WeChatMsgItemPreview() {
    WeChatMsgItem(
        WeChatMsg(R.drawable.image2, "金甲工坊", "今天下午3点在技能训练中心开周例会", "11:30"),
    )
}
